using System;
using System.Collections.Generic;
using Infinity.Common;

namespace Infinity.API.Core
{
	public abstract class CodeAction
	{
		private Action Action { get; set; }

		protected CodeAction()
		{
			Action = () => { };
		}

		/// <summary>
		/// Default constructor
		/// </summary>
		protected CodeAction(Action action)
		{
			Ensure.NotNull(action);
			Action = action;
		}

		/// <summary>
		/// Run action
		/// </summary>
		public abstract void Run();

		/// <summary>
		/// It is invoked before run
		/// </summary>
		public event Action<CodeAction> Executing;

		/// <summary>
		/// It is invoked after run
		/// </summary>
		public event Action<CodeAction> Executed;

		protected void OnExecuting(CodeAction obj)
		{
			Action<CodeAction> handler = Executing;
			if (handler != null) 
				handler(obj);
		}

		protected void OnExecuted(CodeAction sender)
		{
			Action<CodeAction> handler = Executed;
			if (handler != null) 
				handler(sender);
		}
	}
}